<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/10
 * Time: 21:57
 */

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\OddsHandicap;
use Carbon\Carbon;
use App\Game\Traits\TcgTrait;
use Illuminate\Support\Facades\Log;

class tcg extends Game
{
    use TcgTrait;

    private $platName = 'tcg';

    private $productType = 2;



    public function __construct()
    {

        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'lottery',
            'status' => 1
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array|mixed
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $registerParams = [
            'username' => $accountUserName,
            'currency' => $this->currency,
            'method' => 'cm',
            'password' => $password
        ];

        $result = $this->send_require($registerParams);
        $result = json_decode($result, true);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'tcg...'.json_encode($result).PHP_EOL, FILE_APPEND);
        if ($result['status'] == 0) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => $result['error_desc'], 'message' => ''];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {
        $getBalanceParams = [
            'username' => $accountUserName,
            'method' => 'gb',
            'product_type' => $this->productType
        ];
        $result = $this->send_require($getBalanceParams);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            return ['success' => true, 'data' => ['balance' => $result['balance'],], 'message' => ''];
        } else {
            return ['success' => false, 'data' => $result['error_desc'], 'message' => ''];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $getBalanceParams = [
            'username' => $accountUserName,
            'method' => 'ft',
            'product_type' => $this->productType,
            'fund_type' => 1,
            'amount' => $price,
            'reference_no' => $transferid
        ];
        $result = $this->send_require($getBalanceParams);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            if ($result['transaction_status'] == "SUCCESS") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => $result['transaction_status']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => $result['error_desc']];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $getBalanceParams = [
            'username' => $accountUserName,
            'method' => 'ft',
            'product_type' => $this->productType,
            'fund_type' => 2,
            'amount' => $price,
            'reference_no' => $transferid
        ];
        $result = $this->send_require($getBalanceParams);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            if ($result['transaction_status'] == "SUCCESS") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => $result['transaction_status']];
            }

        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => $result['error_desc']];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        $getBalanceParams = [
            'method' => 'cs',
            'product_type' => $this->productType,
            'ref_no' => $request->transferid
        ];
        $result = $this->send_require($getBalanceParams);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            if ($result['transaction_status'] == "SUCCESS") {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            } elseif($result['transaction_status'] == "FAILED") {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => $result['transaction_status']];
            }else{
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => $result['transaction_status']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => $result['error_desc']];
        }
    }

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array|mixed
     */
    public function curlJoinGame($request)
    {
        $gameMode = (bool)($request->demo) ? 0 : 1;
        $lottery_bet_mode = 'Elott';
        $view = $request->is_lobby ? 'Lobby' : 'Betting';

        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $odds = isset($oddType) ? $oddType->plat_code : 'A';
        $series = config('lottery.' . $this->platName . '.' . $odds);

        $params = [
            'username' => $request->accountUserName,
            'method' => 'lg',
            'product_type' => $this->productType,
            'game_code' => $request->gamecode,
            'game_mode' => $gameMode,
            'platform' => 'flash',
            'lottery_bet_mode' => $lottery_bet_mode,
            'view' => $view,
            'series' => $series
        ];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'tcg...'.json_encode($params).PHP_EOL, FILE_APPEND);

        $result = $this->send_require($params);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            if (isset($result['pt_username'])) {
                return [
                    'success' => true,
                    'data' => [
                        'accountUserName' => $result['pt_username'],
                        'password' => $result['pt_password'],
                        'gamecode' => $request->gamecode
                    ],
                    'message' => ''
                ];
            }
            return ['success' => true, 'data' => ['url' => $this->joinGame . $result['game_url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => $result['error_desc'], 'message' => ''];
        }

        return  ['success' => false, 'data' => '系统错误', 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $gameMode = (bool)($request->demo) ? 0 : 1;
        $lottery_bet_mode = 'Elott_Traditional_Mobile_V2';
        $view = $request->is_lobby ? 'Game_List' : 'Betting';
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $odds = isset($oddType) ? $oddType->plat_code : 'A';
        $series = config('lottery.' . $this->platName . '.' . $odds);
        $params = [
            'username' => $request->accountUserName,
            'method' => 'lg',
            'product_type' => $this->productType,
            'game_code' => $request->gamecode,
            'game_mode' => $gameMode,
            'platform' => 'html5',
            'lottery_bet_mode' => $lottery_bet_mode,
            'view' => $view,
            'series' => $series
        ];

        $result = $this->send_require($params);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            if (isset($result['pt_username'])) {
                return [
                    'success' => true,
                    'data' => [
                        'accountUserName' => $result['pt_username'],
                        'password' => $result['pt_password'],
                        'gamecode' => $request->gamecode
                    ],
                    'message' => ''
                ];
            }
            return ['success' => true, 'data' => ['url' => $this->joinGame . $result['game_url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => $result['error_desc'], 'message' => ''];
        }

          return  ['success' => false, 'data' => '系统错误', 'message' => ''];
    }


    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'tcg...'.PHP_EOL, FILE_APPEND);
        $data = [];
        $params = ['method' => 'elsbd',];
        $batches = $this->getBatchArray($startTime, $endTime);

        if (!empty($batches)) {
            foreach ($batches as $batch) {
                $totalPage = $page = 1;
                do {
                    $params['page'] = $page;
                    $params['batch_name'] = $batch;
//                    file_put_contents('/home/gameapi/storage/logs/test.log', 'ppp...'.json_encode($params).PHP_EOL, FILE_APPEND);
                    $response = $this->send_require($params);
                    $response = json_decode($response, true);
//                    file_put_contents('/home/gameapi/storage/logs/test.log', 'response...'.json_encode($response).PHP_EOL, FILE_APPEND);

                    if ($response['status'] == 0) {
                        $totalPage = $response['page_info']['totalPage'];
                        $data = array_merge($data, $response['details']);
                    }
                    $page++;
                } while ($page <= $totalPage);
                sleep(3);
            }
        }
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'tcg...'.json_encode($data).PHP_EOL, FILE_APPEND);
        return $data;

    }


}